﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using bts_webapp.Models;

namespace bts_webapp
{
    public partial class ProblemManagement : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormItem)
            {
                UxProblem uxProblem = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxProblem;
                var problemUpdate = uxProblem.GetData();
                var problem = this.DbContext.VIBA_SUCOs.FirstOrDefault(u => u.SuCo_Id == problemUpdate.SuCo_Id);
                if (problem != null)
                {
                    problem.SuCo_Id = problemUpdate.SuCo_Id;
                    problem.Tram_Id = problemUpdate.Tram_Id;
                    problem.Luong_Id = problemUpdate.Luong_Id;
                    problem.ThietBi_Id = problemUpdate.ThietBi_Id;
                    problem.User_Id = problemUpdate.User_Id;
                    problem.SuCo_TenSuCo = problemUpdate.SuCo_TenSuCo;
                    problem.SuCo_NoiDungSuCo = problemUpdate.SuCo_NoiDungSuCo;
                    problem.SuCo_NgayGio = problemUpdate.SuCo_NgayGio;
                    problem.SuCo_GhiChu = problemUpdate.SuCo_GhiChu;
                    problem.Status = problemUpdate.Status;

                    this.DbContext.SubmitChanges();
                }

            }
        }

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {

        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.RadGrid1.DataSource = this.DbContext.VIBA_SUCOs.ToList();
        }

        protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                var problemId = (e.Item as GridDataItem)["SuCo_Id"].Text;
                var problem = this.DbContext.VIBA_SUCOs.FirstOrDefault(u => u.SuCo_Id == Convert.ToInt32( problemId));
                if (problem != null)
                {
                    this.DbContext.VIBA_SUCOs.DeleteOnSubmit(problem);
                    this.DbContext.SubmitChanges();
                }
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            var problem = e.Item.DataItem as VIBA_SUCO;

            //insert new station
            if (e.Item is GridEditFormInsertItem)
            {
                UxProblem uxProblem = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxProblem;
                uxProblem.AddComboBoxTramItems();
                uxProblem.AddComboBoxLuong();
                uxProblem.AddComboBoxThietbi();
                uxProblem.AddComboBoxUser();
                uxProblem.EnableFieldCode(true);
                uxProblem.InsertOrUpdate = true;
            }
            //update user
            else if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                UxProblem uxProblem = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxProblem;
                uxProblem.AddComboBoxTramItems();
                uxProblem.AddComboBoxLuong();
                uxProblem.AddComboBoxThietbi();
                uxProblem.AddComboBoxUser();
                uxProblem.BindData(problem);
                uxProblem.EnableFieldCode(false);
                uxProblem.InsertOrUpdate = false;
            }

            if (e.Item is GridDataItem)
            {
                var dataItem = e.Item as GridDataItem;
                dataItem["Status"].Text = GetStatus(dataItem["Status"].Text);
                dataItem["Tram_Name"].Text = GetStationName(problem.Tram_Id.ToString());
                dataItem["Luong_Name"].Text = GetLuongName(problem.Luong_Id.ToString());
                dataItem["ThietBi_Name"].Text = GetThietbiName(problem.ThietBi_Id.ToString());
                dataItem["User_Name"].Text = GetUserName(problem.User_Id.ToString());

            }
        }

        private string GetStationName(int? nullable)
        {
            throw new NotImplementedException();
        }
        
        private List<VIBA_TRAM> _stationList;
        public List<VIBA_TRAM> StationList
        {
            get
            {
                if (_stationList == null)
                    _stationList = new BTSModelDataContext().VIBA_TRAMs.ToList();
                return _stationList;
            }
            set
            {
                _stationList = value;
            }
        }

        private string GetStationName(string stationId)
        {
            var st = this.StationList.FirstOrDefault(x => x.Tram_Id.Trim() == stationId.Trim());
            if (st != null)
                return st.Tram_Ten;
            return string.Empty;
        }

        private List<VIBA_THIET_BI> _thietbiList;
        public List<VIBA_THIET_BI> ThietbiList
        {
            get
            {
                if (_thietbiList == null)
                    _thietbiList = new BTSModelDataContext().VIBA_THIET_BIs.ToList();
                return _thietbiList;
            }
            set
            {
                _thietbiList = value;
            }
        }

        private string GetThietbiName(string thietbiId)
        {
            var st = this.ThietbiList.FirstOrDefault(x => x.ThietBi_Id.Trim() == thietbiId.Trim());
            if (st != null)
                return st.ThietBi_Ten;
            return string.Empty;
        }

        private List<VIBA_LUONG> _luongList;
        public List<VIBA_LUONG> LuongList
        {
            get
            {
                if (_luongList == null)
                    _luongList = new BTSModelDataContext().VIBA_LUONGs.ToList();
                return _luongList;
            }
            set
            {
                _luongList = value;
            }
        }

        private string GetLuongName(string luongId)
        {
            var st = this.LuongList.FirstOrDefault(x => x.Luong_Id.Trim() == luongId.Trim());
            if (st != null)
                return st.Luong_Ten;
            return string.Empty;
        }

        private List<VIBA_User> _userList;
        public List<VIBA_User> UserList
        {
            get
            {
                if (_userList == null)
                    _userList = new BTSModelDataContext().VIBA_Users.ToList();
                return _userList;
            }
            set
            {
                _userList = value;
            }
        }

        private string GetUserName(string userId)
        {
            if (userId == null)
                userId = "";
            var st = this.UserList.FirstOrDefault(x => x.User_Id.Trim() == userId.Trim());
            if (st != null)
                return st.User_Full_Name;
            return string.Empty;
        }
        
        
        private string GetStatus(string status)
        {
            return status == "1" ? "<font color='blue'>Hoạt động</font>" : "<font color='red'>Không hoạt động</font>";
        }

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {

        }

        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem)
            {
                UxProblem uxProblem = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxProblem;
                var problem = uxProblem.GetData();
                if (!IsExists(problem.SuCo_Id.ToString()))
                {
                    this.DbContext.VIBA_SUCOs.InsertOnSubmit(problem);
                    this.DbContext.SubmitChanges();
                    uxProblem.SetErrorStatus(string.Empty);
                }
                else
                {
                    uxProblem.SetErrorStatus("Mã sự cố đã tồn tại, xin chọn mã khác.");
                    e.Canceled = true;
                }
            }
        }

        private bool IsExists(string problemId)
        {
            var context = new BTSModelDataContext();
            var item = context.VIBA_SUCOs.FirstOrDefault(x => x.SuCo_Id == Convert.ToInt32(problemId.ToLower()));
            return item != null;
        }
    }
}